package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.GoodsVo;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Comment;
import com.gxa.autoparts.core.pojo.Goods;
import com.gxa.autoparts.core.pojo.GoodsSku;
import com.gxa.autoparts.core.util.StringUtil;
import com.gxa.autoparts.portal.service.GoodService;
import com.gxa.autoparts.portal.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URISyntaxException;
import java.util.*;

/**
 * @author ：looker
 * @date ：Created in 2022/1/6 9:06
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品控制器")
public class GoodsController {

    @Autowired
    GoodService goodService;

    @Autowired
    SearchService searchService;

    @PostMapping("/searchById")
    @ApiOperation("根据商品Id获取商品详情页面数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = true,dataType = "long",paramType = "query")
    })
    public Map searchById(@RequestParam("id") Long id) throws BusinessException, URISyntaxException {
        if(id==null){
            throw new BusinessException(ExceptionCode.ID_NULL);
        }
        Map map = goodService.searchById(id);
        JsonResult result = new JsonResult();
        result.setCode("200");
        result.setData(map);
        return result.getDataMap();
    }

    @PostMapping("/getSearchHistory")
    @ApiOperation("获取历史搜索记录")
    public Map getSearchHistory(){
        Set<Object> searchHistoryByUserId = searchService.getSearchHistoryByUserId();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(searchHistoryByUserId);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getHotSearchList")
    @ApiOperation("获取热度搜索词")
    public Map getHotSearchList(){
        Set<Object> hotList = searchService.getHotList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(hotList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/searchGoodByWords")
    @ApiOperation("首页模糊搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "words",value = "模糊词",required = false,dataType = "long",paramType = "query")
    })
    public Map searchGoodByWords(String words){
        if(!("".equals(words))){
            searchService.searchWord(words);
        }
        List<GoodsVo> goodsVos = goodService.searchByWord(words);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(goodsVos);
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/searchGoodsCommon")
    @ApiOperation("综合检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "words",value = "模糊词",required = false,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "typeId",value = "筛选类型Id",required = false,dataType = "long",paramType = "query")
    })
    public Map searchGoodsCommon(String words,Long typeId){
        if(!("".equals(words))){
            searchService.searchWord(words);
        }
        List<GoodsVo> goodsVos = goodService.searchCommon(words, typeId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(goodsVos);
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }


    @PostMapping("/searchGoodsBySaleCount")
    @ApiOperation("销量检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "words",value = "模糊词",required = false,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "typeId",value = "筛选类型Id",required = false,dataType = "long",paramType = "query")
    })
    public Map searchGoodsBySaleCount(String words,Long typeId){
        if(!("".equals(words))){
            searchService.searchWord(words);
        }
        List<GoodsVo> goodsVos = goodService.searchBySaleCount(words, typeId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(goodsVos);
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }


    @PostMapping("/searchGoodsByPriceSort")
    @ApiOperation("价格检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "words",value = "模糊词",required = false,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "typeId",value = "筛选类型Id",required = false,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "价格筛选升降序",required = true,dataType = "boolean",paramType = "query")
    })
    public Map searchGoodsByPriceSort(String words,Long typeId,Boolean sort){
        if(!("".equals(words))){
            searchService.searchWord(words);
        }
        if(sort ==null){
            sort = true;
        }
        List<GoodsVo> goodsVos = goodService.searchByPriceSort(words, typeId,sort);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(goodsVos);
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }


    @PostMapping("/saveGoods")
    public void updateGoods(){
        goodService.updateGoods();
    }


    /**
     * 假接口
     * @return
     */
    @PostMapping("/getHeads")
    @ApiOperation("首页数据")
    public Map getHotGoods(){
        List<Goods> bannerGoods1 = new ArrayList<>();


        Goods goods = new Goods();
        goods.setId(1L);
        goods.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/a83cb260e7114b31b40e637f66df15c2.png");
        goods.setName("垃圾护手器");
        goods.setSaleCount(1000);
        goods.setSalePrice(120.0);
        bannerGoods1.add(goods);

        Goods goods2 = new Goods();
        goods2.setId(2L);
        goods2.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/6e365a85142d411793f8ea95dffa4f14.jpg");
        goods2.setName("自拍长杆");
        goods2.setSalePrice(50.0);
        goods2.setSaleCount(1200);
        bannerGoods1.add(goods2);

        Goods goods3 = new Goods();
        goods3.setId(3L);
        goods3.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/45c632af5fde4a0682c250830c6ee810.png");
        goods3.setName("瓜瓜神器");
        goods3.setSalePrice(78.0);
        goods3.setSaleCount(1320);
        bannerGoods1.add(goods3);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("banner",bannerGoods1);
        jsonResult.put("secondKill",bannerGoods1);
        jsonResult.put("hot",bannerGoods1);
        return jsonResult.getDataMap();

        //真实数据
//        HashMap<Object, Object> map = new HashMap<>();
//        List<Goods> bannerGoods = goodService.getBannerGoods();
//        map.put("bannerGoods",bannerGoods);
//        List<Goods> hotGoods = goodService.getHotGoods();
//        map.put("hotGoods",hotGoods);
//        List<Goods> secondKillGoods = goodService.getSecondKillGoods();
//        map.put("secondKillGoods",secondKillGoods);
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode("200");
//        jsonResult.setData(map);
//        return jsonResult.getDataMap();
    }

    @PostMapping("/getSecondKillTime")
    @ApiOperation("获取秒杀时间")
    public Map getSecondKillTime(){
        List<String> objects = new ArrayList<>();
        objects.add("8:00");
        objects.add("10:00");
        objects.add("12:00");
        objects.add("14:00");
        objects.add("16:00");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(objects);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getSecondKillGoods")
    @ApiOperation("获取秒杀商品")
    public Map getSecondKillGoods(String time){
        List<Goods> bannerGoods1 = new ArrayList<>();


        Goods goods = new Goods();
        goods.setId(1L);
        goods.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/a83cb260e7114b31b40e637f66df15c2.png");
        goods.setName("垃圾护手器");
        goods.setSaleCount(1000);
        goods.setRemarkPrice(141.0);
        goods.setSalePrice(120.0);
        bannerGoods1.add(goods);

        Goods goods2 = new Goods();
        goods2.setId(2L);
        goods2.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/6e365a85142d411793f8ea95dffa4f14.jpg");
        goods2.setName("自拍长杆");
        goods2.setSalePrice(50.0);
        goods2.setSaleCount(1200);
        goods2.setRemarkPrice(80.0);
        bannerGoods1.add(goods2);

        Goods goods3 = new Goods();
        goods3.setId(3L);
        goods3.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/45c632af5fde4a0682c250830c6ee810.png");
        goods3.setName("瓜瓜神器");
        goods3.setSalePrice(78.0);
        goods3.setRemarkPrice(90.0);
        goods3.setSaleCount(1320);
        bannerGoods1.add(goods3);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(bannerGoods1);
        return jsonResult.getDataMap();
    }


    @PostMapping("/getTimeLimit")
    @ApiOperation("获取限时抢购时间")
    public Map getTimeLimit(){
        List<String> objects = new ArrayList<>();
        objects.add("8:00");
        objects.add("10:00");
        objects.add("12:00");
        objects.add("14:50");
        objects.add("18:00");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(objects);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getTimeLimitGoods")
    @ApiOperation("获取限时秒杀商品")
    public Map getTimeLimitGoods(String time){
        List<Goods> bannerGoods1 = new ArrayList<>();


        Goods goods = new Goods();
        goods.setId(1L);
        goods.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/a83cb260e7114b31b40e637f66df15c2.png");
        goods.setName("垃圾护手器");
        goods.setSaleCount(1000);
        goods.setRemarkPrice(141.0);
        goods.setSalePrice(120.0);
        bannerGoods1.add(goods);

        Goods goods2 = new Goods();
        goods2.setId(2L);
        goods2.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/6e365a85142d411793f8ea95dffa4f14.jpg");
        goods2.setName("自拍长杆");
        goods2.setSalePrice(50.0);
        goods2.setSaleCount(1200);
        goods2.setRemarkPrice(80.0);
        bannerGoods1.add(goods2);

        Goods goods3 = new Goods();
        goods3.setId(3L);
        goods3.setImg("https://bucket-dengbangwu.oss-cn-chengdu.aliyuncs.com/images/uploadImg/45c632af5fde4a0682c250830c6ee810.png");
        goods3.setName("瓜瓜神器");
        goods3.setSalePrice(78.0);
        goods3.setRemarkPrice(90.0);
        goods3.setSaleCount(1320);
        bannerGoods1.add(goods3);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(bannerGoods1);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getStand")
    @ApiOperation("获取商品规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodId",value = "商品Id",required = true,dataType = "long",paramType = "query")})
    public Map getStand(Long goodId) throws BusinessException {
        List<Map<String, Object>> standards = goodService.getStandards(goodId);
        List<Map<String, Object>> skuJsonList = goodService.getSkuJsonList(goodId);
        System.out.println(standards);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("stands",standards);
        jsonResult.put("skuJsonList",skuJsonList);
        return jsonResult.getDataMap();
    }


    @PostMapping("/orderTake")
    @ApiOperation("加入购物车")
    public Map orderTake(Long id,Integer num){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功加入购物车");
        return jsonResult.getDataMap();
    }


}
